var e = getApp(), t = e.requirejs("core"), a = e.requirejs("wxParse/wxParse"), i = e.requirejs("biz/diypage"), r = e.requirejs("jquery");

//index.js
//获取应用实例
var app = getApp();
var calendarSignData;
var date;
var calendarSignDay;
Page({
  data:{
    jifenshow:true,
    qiandaoshow: true,
    mask1:true,
    mask2: true,
    qiandao:[],
    jifenrole:null,
    jifen:null
  },
  // 签到记录显示隐藏
  qiandao:function(){
    var that = this
    t.loading();
    t.post("sign/index/getRecords", {
      openid: 1213,
    }, function (e) {
      console.log(e);
      if (e.status == 0) {
        // e.alert(e.result.message);
        t.toast(e.result.message, "loading");
      } else {
          that.setData({
            qiandao:e.result.list
          })
      }
      t.hideLoading();
    })
    that.setData({
      qiandaoshow: !this.data.qiandaoshow,
      mask1: !this.data.mask1
    })
  },
  // 积分攻略显示隐藏
  jifen: function () {
    var that = this
    that.setData({
      jifenshow: !this.data.jifenshow,
      mask2: !this.data.mask2
    })
  },
  //事件处理函数
  calendarSign: function () {
    var that = this;
    
    var calendarSignData = this.data.calendarSignData

    calendarSignData[date] = date;
   
    // wx.setStorageSync("calendarSignData", calendarSignData);
    t.loading();
    t.post("sign/index/dosign", {
      calendarSignData: calendarSignData,
    }, function (e) {
      console.log(e);
      if(e.status == 0){
        // e.alert(e.result.message);
        t.toast(e.result.message, "loading");
      }else{
        that.setData({
          calendarSignData: calendarSignData,
          jifen: e.result.addcredit+'微币',
          calendarSignDay: that.data.calendarSignDay+1
        })
        wx.showToast({
          title: '签到成功',
          icon: 'success',
          duration: 2000,
        })
      }
      // t.hideLoading();
    })
    // return false;
  
    
  
  },
  onLoad: function () {
    var mydate = new Date();
    var year = mydate.getFullYear();
    var month = mydate.getMonth() + 1;
    date = mydate.getDate();
    // console.log("date" + date + '返回今天日期')
    var day = mydate.getDay() + 1;
    // console.log(day + '返回今天是周几')

    var nbsp = 7 - ((date - day) % 7);
    console.log("nbsp" + nbsp);
    var monthDaySize;
    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
      monthDaySize = 31;
    } else if (month == 4 || month == 6 || month == 9 || month == 11) {
      monthDaySize = 30;
    } else if (month == 2) {
      // 计算是否是闰年,如果是二月份则是29天
      if ((year - 2000) % 4 == 0) {
        monthDaySize = 29;
      } else {
        monthDaySize = 28;
      }
    };

    var that = this;
    t.loading();
    t.post("sign/index/main", {
    }, function (e) {
      console.log(e);
      if (e.status == 0) {
        // e.alert(e.result.message);
        t.toast(e.result.message, "loading");
      } else {
        console.log(e.status.texts);
        console.log('------------------------------------------------')
        a.wxParse("wxParseData", "html", e.status.texts.sign_rule, that, "0");
        if (!e.status.records.json_day){
            wx.setStorageSync("calendarSignData", new Array(monthDaySize))
            calendarSignData = wx.getStorageSync("calendarSignData")
            e.status.records.json_day = calendarSignData
        }
        console.log(e.status.records.json_day);
        console.log('-----------------签到日期------------------------------')
        that.setData({
          // date: e.status.json_arr.calendar,
          calendarSignDay: e.status.json_arr.signinfo.sum,
          jifenrole: e.status.texts.sign_rule,
          calendarSignData:e.status.records.json_day,
          jifen:e.status.records.jifen,
          year: year,
          month: month,
          nbsp: nbsp,
          monthDaySize: monthDaySize,
          date: date,
        })
        //wx.setStorageSync("calendarSignData", e.status.records.json_day);
      }
      t.hideLoading();
    })
  

    
    
  }
})
